﻿<?php
/**
 * This is the file to define HomeOverview.
 * 
 * @author	Justin Yoo
 * @version	0.0.1.0001
 * @package	KYR.Components
 * 
 */

//	Checks if this file is called within the framework; otherwise terminates.
if (!defined("IS_FRAMEWORK") || !IS_FRAMEWORK)	header("Location: http://".$_SERVER["HTTP_HOST"]."/error/404");

//	Imports relevant files.
require_once CORE_PATH."KYR.Core.BaseComponent.php";

/**
 * This is class HomeOverview that overviews contents.
 *
 * Overviews contents.
 * 
 * @author	Justin Yoo
 * @package	KYR.Components
 * 
 */
class HomeOverview extends BaseComponent
{
	/**
	 * Initiates the HomeOverview class.
	 *
	 * @access	public
	 * @param	Request	$request	Request object that contains page request details.
	 * @param	int		$objectID	Either moduleID or widgetID that calls the component.
	 * @param	string	$mappedBy	Flag whether the component is called by a module or widget.
	 * @param	int		$statusCode	Page status code.
	 * @exception					Throws an Exception while initialising the class.
	 *
	 */
	public function __construct($request, $objectID, $mappedBy, $statusCode = 200)
	{
		parent::__construct($request, $objectID, $mappedBy, $statusCode);
		$this->componentName = __CLASS__;
		if ($this->statusCode == 200)
		{
			try
			{
				$this->CheckComponent_();
				if (!$this->existComponent)
					throw new Exception("Component not found");
			}
			catch (Exception $ex) { throw $ex; }
		}
		else
			$this->existComponent = true;
		//	Creates controls for this component.
		$this->CreateControls_();
	}
	
	/**
	 * Creates a set of controls to be used in templates called by the component.
	 *
	 * @access	protected
	 *
	 */
	protected function CreateControls_()
	{
		//	Create controls for this component.
		$this->controls_ = array(
			"title"				=> array("type" => "text",		"value" => null, "maxlength" => null,	"validation" => null),
			"image"				=> array("type" => "text",		"value" => null, "maxlength" => null,	"validation" => null),
			"imageDescription"	=> array("type" => "text",		"value" => null, "maxlength" => null,	"validation" => null),
			"imageCredit"		=> array("type" => "text",		"value" => null, "maxlength" => null,	"validation" => null),
			"content"			=> array("type" => "text",		"value" => null, "maxlength" => null,	"validation" => null)
			);
	}
	
	/**
	 * Processes the component.
	 *
	 * @access	public
	 * @param	array	$variables	Set of data to be stored by the component.
	 * @exception					Throws an Exception while processing the component.
	 *
	 */
	public function ProcessComponent(&$variables)
	{
		//	Processes get/post.
		$this->ProcessRequest($variables);
		
		//	Sets the controls.
		$this->SetControls_($variables);

		//	Processes the parent method.
		try { $this->ProcessComponent_($variables); } catch (Exception $ex) { throw $ex; }

		//	Processes handlers
		if (isset($this->handlers) && count($this->handlers) > 0)
			foreach ($this->handlers as $handler)
				$handler->ProcessHandler($variables);
	}

	/**
	 * Processes the requests within the page.
	 *
	 * @access	public
	 * @param	array	$variables	The set of GET/POST variables.
	 * @exception		$ex			Throws an exception while processing the request.
	 *
	 */
	public function ProcessRequest(&$variables)
	{
		try
		{
			if ($this->request->isAjax)
				$this->ProcessAJAX($variables);
			else
				$this->ProcessGetPost($variables);
		}
		catch (Exception $ex) { throw $ex; }
	}
	
	/**
	 * Processes the AJAX requests within the page.
	 *
	 * @access	public
	 * @param	array	$variables	The set of GET/POST variables.
	 * @exception		$ex			Throws an exception while processing the request.
	 *
	 */
	public function ProcessAJAX(&$variables)
	{
		try
		{
		}
		catch (Exception $ex) { throw $ex; }
	}
	
	/**
	 * Processes the GET/POST requests within the page.
	 *
	 * @access	public
	 * @param	array	$variables	The set of GET/POST variables.
	 * @exception		$ex			Throws an exception while processing the request.
	 *
	 */
	public function ProcessGetPost(&$variables)
	{
		try
		{
		}
		catch (Exception $ex) { throw $ex; }
	}

	/**
	 * Sets the set of controls to be used in templates called by the component.
	 *
	 * @access	protected
	 * @param	array	$variables	Set of data generated by the component.
	 * @exception					Throws an Exception while setting data onto the controls.
	 *
	 */
	protected function SetControls_(&$variables)
	{
		//	Gets the content from the database.
		try { $content = $this->_GetContent(); }
		catch (Exeption $ex) { throw $ex; }

		//	Sets the content to the controls.
		$this->controls_["title"]["value"]				= $content["title"];
		$this->controls_["image"]["value"]				= empty($content["contentID"]) ? null : BASE_URL."media/image/".(empty($content["image"]) ? "blank.jpg" : $content["image"]);
		$this->controls_["imageDescription"]["value"]	= $content["imageDescription"];
		$this->controls_["imageCredit"]["value"]		= $content["imageCredit"];
		$this->controls_["content"]["value"]			= $content["content"];

		//	Adds the controls for this component.
		switch ($this->request->actionStep)
		{
			case "view":
				$variables["controls"]["HomeOverview"] = $this->controls_;
				break;
		}
	}
	
	/**
	 * Gets the content based on the status code.
	 *
	 * @access	private
	 * @return	array		Returns the content based on the status code.
	 * @exception			Throws an Exception while retrieving data from the database.
	 *
	 */
	private function _GetContent()
	{
		try
		{
			$db	= null;
			$content = null;
			if ($this->statusCode == 200)
			{
				$db = new Database("MySQL");
				$qry = "CALL cms_GetContentByPageID(";
				$qry .= $this->request->pageID;
				$qry .= ")";
				$db->Query($qry);
				$content = $db->Fetch();
			}
			else
			{
				$title = $this->statusCode;
				switch ($this->statusCode)
				{
					case 404:
						$title		.= " - Page Not Found";
						$content	= "<p>The page does not exist or has been temporarily removed. Please try another one. We apoligise any inconvenience caused.</p>";
						break;
					case 500:
						$title		.= " - Internal Server Error";
						$content	= "<p>There is a server error while processing your request. Please try again. We apoligise any inconvenience caused.</p>";
						break;
					case 503:
						$title		.= " - Connection Refused";
						$content	= "<p>The web site is temporarily unavailable. Please try it later again. We apoligise any inconvenience caused.</p>";
						break;
				}
				$content = array(
					"contentID"			=> null,
					"title"				=> $title,
					"content"			=> $content,
					"image"				=> null,
					"imageDescription"	=> null,
					"imageCredit"		=> null
					);
			}
			return $content;
		}
		catch (Exception $ex) { throw $ex; }
	}
}
?>
